/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.skatiger.jrove.el;

import java.lang.reflect.Method;

import javax.el.ArrayELResolver;
import javax.el.BeanELResolver;
import javax.el.CompositeELResolver;
import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.FunctionMapper;
import javax.el.ListELResolver;
import javax.el.MapELResolver;
import javax.el.ResourceBundleELResolver;
import javax.el.VariableMapper;

import org.skatiger.jrove.ViewContext;
import org.skatiger.jrove.el.implicaitobject.JroveImplicitObjectResolver;
import org.skatiger.jrove.util.AssertUtil;

/**
 * jrove el Context
 * 
 * @author skatiger
 * 
 */
public class JroveELContext extends ELContext {

    private final static FunctionMapper NullFunctionMapper = new FunctionMapper() {
        public Method resolveFunction(String prefix, String localName) {
            return null;
        }
    };

    private final CompositeELResolver _compositeELResolver;

    private FunctionMapper functionMapper;

    private VariableMapper variableMapper;

    public JroveELContext(ViewContext viewContext) {
        AssertUtil.notNull(viewContext, "viewContext must not be null.");
        _compositeELResolver = buildELResolver(viewContext);
        functionMapper = NullFunctionMapper;
        putContext(ViewContext.class, viewContext);
    }

    protected CompositeELResolver buildELResolver(ViewContext viewContext) {
        CompositeELResolver compositeELResolver = new CompositeELResolver();
        compositeELResolver.add(new MapELResolver());
        compositeELResolver.add(new ListELResolver());
        compositeELResolver.add(new ArrayELResolver());
        compositeELResolver.add(new BeanELResolver());
        compositeELResolver.add(new ResourceBundleELResolver());
        // add view scope attributes ELResolver
        compositeELResolver.add(new ViewScopeELResolver());

        compositeELResolver.add(JroveImplicitObjectResolver.makeResolver());

        ELResolver[] resolvers = viewContext.getConfigurator().getELResolvers();
        for (int i = 0; i < resolvers.length; i++) {
            compositeELResolver.add(resolvers[i]);
        }
        return compositeELResolver;
    }

    public void appendELResolver(ELResolver resolver) {
        _compositeELResolver.add(resolver);
    }

    public ELResolver getELResolver() {
        return this._compositeELResolver;
    }

    public FunctionMapper getFunctionMapper() {
        return this.functionMapper;
    }

    public VariableMapper getVariableMapper() {
        if (this.variableMapper == null) {
            this.variableMapper = new VariableMapperImpl();
        }
        return this.variableMapper;
    }

    public void setFunctionMapper(FunctionMapper functionMapper) {
        if (this.functionMapper == null) {
            this.functionMapper = new FunctionMapperImpl();
        }
        this.functionMapper = functionMapper;
    }

    public void setVariableMapper(VariableMapper variableMapper) {
        this.variableMapper = variableMapper;
    }

}
